﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

/// <summary>
/// Summary description for Security
/// </summary>
public static class Security
{
    public static void CreateCookie(string username, string roles)
    {
        try
        {
            //Crete FormsAuthenticationTicket
            FormsAuthenticationTicket fat = new FormsAuthenticationTicket(
                                                            1,
                                                            username,
                                                            DateTime.Now,
                                                            DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout),
                                                            false,
                                                            roles);

            //String encrypt the ticket
            string encThicket = FormsAuthentication.Encrypt(fat);

            //Create the cookie
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encThicket);
            cookie.Path = FormsAuthentication.FormsCookiePath;
            HttpContext.Current.Response.Cookies.Add(cookie);


        }
        catch (Exception)
        {
            throw;
        }
    }

    //TODO: Tova s pazaneto na UserID v sesiyata ne e mnogo dobre zashtoto izchezva pri recycle na app pool-a
    public static decimal GetCurrentUserID()
    {
        return (decimal)HttpContext.Current.Session["UserID"];

    }
}